{$CLEO .cs}
{$USE CLEO+}
{$USE newOpcodes}
{$USE audio}
{$USE ini}

0000: NOP

script_name "Hdbrake"

// By ArtemQa146

if
    0AA2: 31@ = load_dynamic_library "CLEO+.cleo"
then
    if
        0AA4: 30@ = get_dynamic_library_procedure "GetCleoPlusVersion" library 31@
    then
        0AA7: call_function_return 30@ num_params 0 pop 0 29@
        if
            not 29@ >= 0x1020000
        then
            0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the DrIVe mod is disabled" time 15000
            0AA3: free_dynamic_library 31@
            0A93: terminate_this_custom_script
        end
        0AA3: free_dynamic_library 31@
    end
else
    0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the DrIVe mod is disabled" time 15000
    0AA3: free_dynamic_library 31@
    0A93: terminate_this_custom_script
end

const
    Check = 31@
    Check_Global = 30@
    audio_str = 29@
    car_audio = 28@
    audio_volume = 27@
    key_pad = 26@
    right_hand = 25@
    car_x = 24@
end

0AF2: audio_volume = get_float_from_ini_file "cleo\handbrake.ini" section "settings" key "Volume"
0AF0: key_pad = read_int_from_ini_file "cleo\handbrake.ini" section "settings" key "Gamepad Key"

while true
wait 0
    if
        player.Defined(0)
    then
    gosub @Check_Global
    if
        Check_Global == true
    then
    
        audio_str = -1
        car_audio = Actor.CurrentCar($PLAYER_ACTOR)
        0AC1: audio_str = load_audio_stream_with_3d_support "CLEO/SoundsQa/hdbrake.mp3" // IF and SET
        0AC5: link_3d_audio_stream audio_str to_car car_audio
        
        0441: 5@ = car car_audio model
        0AD3: 10@v = string_format "%d" 5@
        0AF0: right_hand = get_int_from_ini_file "CLEO\handbrake.ini" section "right hand drive car" key 10@v
        
        0085: car_X = car_audio // (int)
    
        repeat
        wait 0
        04ED: load_animation  "DrIVeMod"
        until 04EE:   animation "DrIVeMod" loaded

timera = 0

while true
wait 0
    gosub @Check_Global
        if and
            003B:   car_X == 0@  // (int)
            Check_Global == true
        then
        
                    046C: 1@ = car 0@ driver
                    if
                        803B:   not 1@ == $PLAYER_ACTOR  // (int)
                    then
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "CAR_Sc1_FL"
                        Actor.Animation($PLAYER_ACTOR) == "CAR_Sc2_FL"
                        Actor.Animation($PLAYER_ACTOR) == "CAR_Sc3_FL"
                        Actor.Animation($PLAYER_ACTOR) == "CAR_Sc4_FL"
                    then
                        continue
                    end
                    
                    if or
                        Actor.Animation($PLAYER_ACTOR) == "DrivebyL_L"
                        Actor.Animation($PLAYER_ACTOR) == "DrivebyL_R"
                        Actor.Animation($PLAYER_ACTOR) == "Driveby_L"
                        Actor.Animation($PLAYER_ACTOR) == "Driveby_R"
                        Actor.Animation($PLAYER_ACTOR) == "horn"
                        Actor.Animation($PLAYER_ACTOR) == "CAR_TUNE_RADIO"
                    then
                        continue
                    end

                    if
                        803B:   not car_audio == 0@  // (int)
                    then
                        if not audio_str == -1
                        then 0AAE: remove_audio_stream audio_str
                        end
                        04EF: release_animation "DrIVeMod"
                        break
                    end

                    if
                        0E42: is_char_doing_task_id $PLAYER_ACTOR task_id 1022
                    then
                        timera = 0
                        continue
                    end
                    
                    1@ = false
                    
                    if
                        0A4B:   controls_set_to_joystick
                    then
                        0293: 2@ = get_controller_mode
                        if
                            2@ == 0
                        then
                            if and
                                $10082 == 0
                                00E1:   player 0 pressed_key 6
                            then
                                1@ = true
                            end
                        else
                            if and
                                $10082 == 0
                                00E1:   player 0 pressed_key 16
                            then
                                1@ = true
                            end
                        end
                    else
                        0D4E: 2@ = read_struct 0xB73458 offset 0xC size 2
                        if
                            2@ == 255
                        then
                            1@ = true
                        end
                    end

                    if and
                        1@ == true
                        not is_char_exiting_any_car $Player_actor
                        80E1:   not player 0 pressed_key 15 // / Enter/ F
                    then
                        if
                            right_hand == 1
                        then
                            if
                                Actor.Animation($PLAYER_ACTOR) == "handbrakeL"
                            then
                                0613: 1@ = actor $PLAYER_ACTOR animation "handbrakeL" time
                                if
                                    1@ >= 0.45
                                then
                                    0614: set_actor $player_actor animation "handbrakeL" progress_to 0.5
                                end
                            else
                                0605: actor $Player_actor perform_animation "handbrakeL" IFP "DrIVeMod" framedelta 8.0 loop 0 lockX 0 lockY 0 lockF 0 time 0
                                0AAD: set_audio_stream audio_str state 1
                                0ABC: set_audio_stream audio_str volume audio_volume
                            end
                        else
                            if
                                Actor.Animation($PLAYER_ACTOR) == "handbrake"
                            then
                                0613: 1@ = actor $PLAYER_ACTOR animation "handbrake" time
                                if
                                    1@ >= 0.45
                                then
                                    0614: set_actor $player_actor animation "handbrake" progress_to 0.5
                                end
                            else
                                0605: actor $Player_actor perform_animation "handbrake" IFP "DrIVeMod" framedelta 8.0 loop 0 lockX 0 lockY 0 lockF 0 time 0
                                0AAD: set_audio_stream audio_str state 1
                                0ABC: set_audio_stream audio_str volume audio_volume
                            end
                        end
                    end



                else
                    if not audio_str == -1
                    then 0AAE: remove_audio_stream audio_str
                    end
                    04EF: release_animation "DrIVeMod"
                    break
                end
            end


        end
    end
end

:Check_Global
Check_Global = false
    if and
        Actor.Driving($PLAYER_ACTOR)
        8741:   not actor $PLAYER_ACTOR busted
        not actor.Dead($PLAYER_ACTOR)
        not is_on_cutscene
        not is_on_scripted_cutscene
    then
        if and
            84C8:   not actor $PLAYER_ACTOR driving_flying_vehicle
            847A:   not actor $PLAYER_ACTOR driving_bike
            84A7:   not actor $PLAYER_ACTOR driving_boat
            84A9:   not actor $PLAYER_ACTOR driving_heli
            89AE:   not actor $PLAYER_ACTOR driving_train
        then
            0@ = Actor.CurrentCar($PLAYER_ACTOR)
            0E12: get_vehicle 0@ subclass_to 1@
            if or
                1@ == 1
                1@ == 0
            then
                Check_Global = true
            end
        end
    end
return
